<?php

namespace app\api\controllers;

use yii\web\Controller;
use app\api\Response;
use app\models\GkyQyhSpBusiness;
use app\api\ApprovalMessage;
class BusinessController extends Controller{
    
    function actionCreate($userId){
        $model = new GkyQyhSpBusiness();
        $params=\Yii::$app->request->post();
        $model->proposer_id = $userId;
        $model->approver_id = isset($params['approver_id']) ? $params['approver_id'] : 1;
        $model->members = isset($params['members']) ? $params['members'] : '';
        $model->start_time = isset($params['start_time']) ? $params['start_time'] : '0000-00-00 00:00:00';
        $model->end_time = isset($params['end_time']) ? $params['end_time'] : '0000-00-00 00:00:00';
        $model->reason = isset($params['reason']) ? $params['reason'] : '';
        $model->route = isset($params['route']) ? $params['route'] : '';
        $model->transportation = isset($params['transportation']) ? $params['transportation'] : '';
        $model->eat_live = isset($params['eat_live']) ? $params['eat_live'] : '';
        $model->funding = isset($params['funding']) ? $params['funding'] : '';
        $model->others = isset($params['others']) ? $params['others'] : '';
        $model->create_time = date('Y-m-d H:i:s',time());
        $model->status = 0;
        $model->print_code = 'BUSI'.rand(1000, 9999).time();
        $model->car_num = isset($params['car_num']) ? $params['car_num'] : '';
        $model->driver = isset($params['driver']) ? $params['driver'] : '';
        
        if($model->save()){
             Response::setHeader();
             echo 100;
             ApprovalMessage::sendMessage($model->proposer_id, $model->approver_id,0, '出差');
        }  else {
            Response::setHeader();
             echo -100;
        }
    }
    
    function actionGetBusiness($printCode){
        $result = GkyQyhSpBusiness::getBusiness($printCode);
        Response::setHeader();
        echo json_encode($result);
    }
    
    public function actionAgree($printCode,$opinion) {
        if (GkyQyhSpBusiness::agreeBusiness($printCode,$opinion)) {
            Response::setHeader();
            echo 1;
            $model = GkyQyhSpBusiness::findOne(['print_code' => $printCode]);

            if (\Yii::$app->request->isGet) {
                ApprovalMessage::sendMessage($model->approver_id, $model->proposer_id, 1, '出差', $printCode);
            }
        } else {
            Response::setHeader();
            echo -1;
        }
    }

    public function actionRefuse($printCode,$opinion) {
        if (GkyQyhSpBusiness::refuseBusiness($printCode,$opinion)) {
            Response::setHeader();
            $model = GkyQyhSpBusiness::findOne(['print_code' => $printCode]);
            if (\Yii::$app->request->isGet) {
                ApprovalMessage::sendMessage($model->approver_id, $model->proposer_id, -1, '出差', $printCode);
            }
            echo 1;
        } else {
            Response::setHeader();
            echo -1;
        }
    }
    
}
